home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Boxer / TZUtils / makedb.c < prev    next >
Text File  |  2000-06-23  |  5KB  |  209 lines

  1. #define IGNORE_STDIO_STUBS
  2. #define __string_h
  3.  
  4. #ifdef OLDGCC
  5.  
  6. #include <Common.h>
  7. #include <System/SysAll.h>
  8. #include <UI/UIAll.h>
  9. #include <Unix/sys_types.h>
  10.  
  11. #else
  12.  
  13. #include <PalmOS.h>
  14. #include <PalmCompatibility.h>
  15. #include <Unix/sys_types.h>
  16.  
  17. #endif
  18.  
  19. #include "stringil.h"
  20. #include "stdio2.h"
  21.  
  22. struct prchead {
  23.   char name[32];                //0-32
  24.   short int attr;               //32-33
  25.   short int vers;               //34-35
  26.   long int cr, md, bkt;         //times 36-47
  27.   long int mn, app, sort;       // zero 48-59 - zero for prcs.
  28.   long int type, crea;          //60-67
  29.   long int uidseed, nxrec;      //68-75 - uidseed rand, nxrec zero;
  30.   short int nrecs;              //76-78
  31. } head;
  32.  
  33. struct rsrcdbent {
  34.   long int rsrc;
  35.   short int rsid;
  36.   long int ofst;                //80+10*(nrecs+index)
  37. } *rsrcent;
  38.  
  39. struct normdbent {
  40.   long int ofst, uid;           //80+8*(nrecs+index)
  41. } *dataent;
  42.  
  43. int MakeDatabase(FILE * fd)
  44. {
  45.   LocalID lid, aiid, siid;
  46.   int i;
  47.   UInt16 cardno = 0;
  48.   UInt16 num, attr;
  49.   UInt32 uid, asz, ssz, ofst, count, total;
  50.   DmOpenRef db;
  51.   void *ap;
  52.   char buf[8];
  53.   RectangleType r;
  54.  
  55.   r.topLeft.x = 0, r.topLeft.y = 15, r.extent.x = 160, r.extent.y = 30;
  56.  
  57.   WinEraseRectangle(&r, 0);
  58.   r.topLeft.y = 30, r.extent.y = 15;
  59.  
  60.   FileTell(fd, &total, NULL);
  61.  
  62.   FileControl(fileOpDestructiveReadMode, fd, NULL, NULL);
  63.  
  64.   memset(&head, 0, sizeof(head));
  65.   fread(&head, sizeof(head), 1, fd);
  66.  
  67.   count = sizeof(head);
  68.  
  69.   r.extent.x = 160 * count / total;
  70.   WinDrawRectangle(&r, 0);
  71.  
  72.   WinDrawChars(head.name, strlen(head.name), 0, 15);
  73.  
  74.   if( DmCreateDatabase(cardno, head.name, head.crea, head.type,
  75.                (head.attr & dmHdrAttrResDB)) ) {
  76.     lid = DmFindDatabase(cardno, head.name);
  77.     if (lid)
  78.       DmDeleteDatabase(cardno, lid);
  79.     DmCreateDatabase(cardno, head.name, head.crea, head.type,
  80.              (head.attr & dmHdrAttrResDB)) ;
  81.   }
  82.  
  83.   lid = DmFindDatabase(cardno, head.name);
  84.  
  85.   if (!lid) {
  86.     fclose(fd);
  87.     return 1;
  88.   }
  89.  
  90.   head.attr &= ~dmHdrAttrReadOnly;
  91.   DmSetDatabaseInfo(cardno, lid, NULL, &head.attr, &head.vers, &head.cr,
  92.                     &head.md, &head.bkt, &head.mn, NULL, NULL, NULL, NULL);
  93.  
  94.   WinDrawChars("/", 1, 150, 0);
  95.  
  96.   if (head.attr & dmHdrAttrResDB) {
  97.     rsrcent = MemPtrNew(10 * (head.nrecs + 1));
  98.     fread(rsrcent, 10, head.nrecs, fd);
  99.     count += head.nrecs * 10;
  100.     rsrcent[head.nrecs].ofst = total;
  101.     ofst = rsrcent[0].ofst;
  102.     for (i = 0; i < head.nrecs; i++)
  103.       if (rsrcent[i].ofst > rsrcent[i + 1].ofst)
  104.         break;
  105.   } else {
  106.     dataent = MemPtrNew(8 * (head.nrecs + 1));
  107.     fread(dataent, 8, head.nrecs, fd);
  108.     count += head.nrecs * 8;
  109.     dataent[head.nrecs].ofst = total;
  110.     ofst = dataent[0].ofst;
  111.     for (i = 0; i < head.nrecs; i++)
  112.       if (dataent[i].ofst > dataent[i + 1].ofst)
  113.         break;
  114.   }
  115.  
  116.   r.extent.x = 160 * count / total;
  117.   WinDrawRectangle(&r, 0);
  118.  
  119.   // VALIDATE
  120.   if (i != head.nrecs) {
  121.     DmDeleteDatabase(cardno, lid);
  122.     return 1;
  123.   }
  124.  
  125.   db = DmOpenDatabase(cardno, lid, dmModeReadWrite);
  126.  
  127.   asz = 0;
  128.   ssz = 0;
  129.   if (head.app)
  130.     asz = (head.sort ? head.sort : ofst) - head.app;
  131.   if (head.sort)
  132.     ssz = ofst - head.sort;
  133.  
  134. #define dmfread(bufP, objSize, numObj, stream) \
  135. FileDmRead((stream), bufP, 0, (objSize), (numObj), NULL)
  136.  
  137.   //    fread(buf, 2, 1, fd);
  138.  
  139.  
  140.   WinDrawChars("\\", 1, 155, 0);
  141.  
  142.   if (asz) {
  143.     while (count < head.app)
  144.       count += fread(buf, 1 , head.app - count > 8 ? 8 : head.app - count, fd);
  145.     ap = DmNewHandle(db, asz);
  146.     count += dmfread(MemHandleLock(ap), 1, asz, fd);
  147.     MemHandleUnlock(ap);
  148.     aiid = MemHandleToLocalID(ap);
  149.     DmSetDatabaseInfo(cardno, lid, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  150.                       &aiid, NULL, NULL, NULL);
  151.  
  152.   }
  153.  
  154.   if (ssz) {
  155.     while (count < head.sort)
  156.       count += fread(buf, 1, head.sort - count > 8 ? 8 : head.sort - count, fd);
  157.     ap = DmNewHandle(db, ssz);
  158.     count += dmfread(MemHandleLock(ap), 1, ssz, fd);
  159.     MemHandleUnlock(ap);
  160.     siid = MemHandleToLocalID(ap);
  161.     DmSetDatabaseInfo(cardno, lid, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  162.                       NULL, &siid, NULL, NULL);
  163.   }
  164.  
  165.   r.extent.x = 160 * count / total;
  166.   WinDrawRectangle(&r, 0);
  167.  
  168.   if (head.attr & dmHdrAttrResDB) {
  169.     for (i = 0; i < head.nrecs; i++) {
  170.       ssz = rsrcent[i + 1].ofst - rsrcent[i].ofst;
  171.       ap = DmNewResource(db, rsrcent[i].rsrc, rsrcent[i].rsid, ssz);
  172.       while (count < rsrcent[i].ofst)
  173.     count += fread(buf, 1, rsrcent[i].ofst - count > 8 ? 8 : rsrcent[i].ofst - count, fd);
  174.       count += dmfread(MemHandleLock(ap), 1, ssz, fd);
  175.       MemHandleUnlock(ap);
  176.       DmReleaseResource(ap);
  177.  
  178.       r.extent.x = 160 * count / total;
  179.       WinDrawRectangle(&r, 0);
  180.  
  181.     }
  182.     MemPtrFree(rsrcent);
  183.   } else {
  184.     for (i = 0; i < head.nrecs; i++) {
  185.       ssz = dataent[i + 1].ofst - dataent[i].ofst;
  186.       num = 0xffff;
  187.       ap = DmNewRecord(db, &num, ssz);
  188.       while (count < dataent[i].ofst)
  189.     count += fread(buf, 1, dataent[i].ofst - count > 8 ? 8 : dataent[i].ofst - count, fd);
  190.       count += dmfread(MemHandleLock(ap), 1, ssz, fd);
  191.       MemHandleUnlock(ap);
  192.  
  193.       attr = dataent[i].uid >> 24;
  194.       uid = dataent[i].uid & 0xffffffUL;
  195.       DmSetRecordInfo(db, num, &attr, &uid);
  196.       DmReleaseRecord(db, num, false);
  197.  
  198.       r.extent.x = 160 * count / total;
  199.       WinDrawRectangle(&r, 0);
  200.  
  201.     }
  202.     MemPtrFree(dataent);
  203.   }
  204.  
  205.   DmCloseDatabase(db);
  206.   return 0;
  207.  
  208. }
  209.